import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

import '../app_config.dart';
import '../constants.dart';

///OSS图片压缩
class ResizeUtils {
  //获取视频第一帧
  static const String OSS_VIDEO_FRAMES =
      "?x-oss-process=video/snapshot,t_1,m_fast";

  //图片转png
  static const String OSS_IMAGE_PNG = "?x-oss-process=image/format,png";

  // //oss图片压缩 50*50
  // static const String OSS_RESIZE_50 = "?x-oss-process=image/resize,w_50";

  //oss图片压缩 100*100
  static const String OSS_RESIZE_100 =
      "?x-oss-process=image/format,png/resize,w_100";

  //oss图片压缩 200*200
  static const String OSS_RESIZE_200 =
      "?x-oss-process=image/format,png/resize,w_200";

  //oss图片压缩 300*300
  static const String OSS_RESIZE_300 =
      "?x-oss-process=image/format,png/resize,w_300";

  //oss图片压缩 300*300
  static const String OSS_RESIZE_500 =
      "?x-oss-process=image/format,png/resize,w_500";
}

//网络图片加载
// ignore: non_constant_identifier_names
Widget NetWorkImgView(String? imgUrl,
    {String placeholderImg = 'assets/images/my/my_head.png', //'assets/images/common/placeholder_image.png',
      String resize = '', //oss图片压缩
      BoxFit fit = BoxFit.contain, //oss图片压缩
      double? height,
      double? width,
      PlaceholderWidgetBuilder? placeholder,
      LoadingErrorWidgetBuilder? errorWidget,
      bool showErr = true}) {
  //网络图片路径不正确
  if (imgUrl == null || imgUrl == '') {
    return Image.asset(placeholderImg,
        width: width, height: height, fit: BoxFit.cover);
  }

  if (!imgUrl.startsWith('http')) {
    imgUrl = EnvironmentConst.apiImageBaseUrl + imgUrl;
  }

  //图片压缩
  imgUrl = imgUrl + resize;

  return CachedNetworkImage(
    height: height,
    width: width,
    imageUrl: imgUrl,
    fit: fit,
    httpHeaders: const {'Referer': Constants.refererUrl},
    fadeOutDuration: const Duration(milliseconds: 0),
    fadeInDuration: const Duration(milliseconds: 0),
    placeholderFadeInDuration: const Duration(milliseconds: 0),
    placeholder: null,
    errorWidget: errorWidget ??
            (context, url, error) => showErr
            ? Image.asset(placeholderImg,
            width: width, height: height, fit: BoxFit.cover)
            : SizedBox(width: width, height: height),
  );
}

///居中文本
Widget myText(String data,
    {TextStyle? style,
      int? maxLines,
      TextAlign? textAlign,
      bool? softWrap,
      TextOverflow? overflow,
      TextDirection? textDirection,
      StrutStyle? strutStyle}) {
  strutStyle ??= const StrutStyle(forceStrutHeight: true, height: 1.1);
  return Text(data,
      style: style,
      maxLines: maxLines,
      textAlign: textAlign,
      softWrap: softWrap,
      overflow: overflow,
      textDirection: textDirection,
      strutStyle: strutStyle);
}